<?php
/**
 * Class name  is ConfigFunctionlogController * 
 * @package backend\modules\common\controllers;
 * @author  Womtech  email:chareler@163.com
 * @DateTime 2020-04-14 23:27 
 */

namespace backend\modules\common\controllers;

use Yii;
use common\models\log\ConfigFunctionlog;
use common\models\log\ConfigFunctionlogSearch;
use backend\controllers\BaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use \yii\web\Response;
use yii\helpers\Html;
use common\utils\ToolUtil;

/**
 * ConfigFunctionlogController implements the CRUD actions for ConfigFunctionlog model.
 */
class ConfigFunctionlogController extends BaseController
{
    public function actions()
    {
        return [            
        ];
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                    'bulkdelete' => ['post'],
                    
                ],
            ],
        ];
    }

    /**
     * Lists all ConfigFunctionlog models.
     * @return mixed
     */
    public function actionIndex()
    {    
        /* $searchModel = new ConfigFunctionlogSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams); */

        return $this->render('index', [            
        ]);
    }

    /**
     * Displays  ConfigFunctionlog model list
     * @return mixed
     */
    public function actionList()
    {   

        $searchModel = new ConfigFunctionlogSearch();
        $searchModel->scenario = 'search';
        $rs = $searchModel->search(Yii::$app->request->getBodyParams());


        if ($searchModel->hasErrors()) {
            $return = ToolUtil::returnAjaxMsg(false, '参数有误', ['errors' => $searchModel->errors]);
            return $this->asJson($return);
        }


        return $this->asJson([
            'code' => 0,
            'msg' => '',
            'count' => $rs['count'],
            'data' => $rs['data'],
        ]);






    }


    /**
     * Displays a single ConfigFunctionlog model.
     * @param integer $id
     * @return mixed
     */
    public function actionView()
    {   
        $request = Yii::$app->request;
        

        //如果是表单get提交,如果有参数id,则是更改,没有则是新增
        if ($request->isGet) {
            $id = $this->get('id');
            if (empty($id)) {                
                return $this->render('view', []);
            } else {

                $model = new ConfigFunctionlog();
                $model = $this->findModel($id);
                return $this->render('view', [
                    'model' => $model,
                ]);
            }
        }




        /* $request = Yii::$app->request;
        if($request->isAjax){
            Yii::$app->response->format = Response::FORMAT_JSON;
            return [
                    'title' => "ConfigFunctionlog #".$id,
                    'content' => $this->renderAjax('view', [
                        'model' => $this->findModel($id),
                    ]),
                    'footer' => Html::button(Yii::t('app', 'Close'), ['class' => 'btn btn-default pull-left','data-dismiss' => "modal"]).
                            Html::a(Yii::t('app', 'Edit'), ['update','id' => $id], ['class' => 'btn btn-primary','role' => 'modal-remote'])
                ];    
        }else{
            return $this->render('view', [
                'model' => $this->findModel($id),
            ]);
        }
 */

    }

    /**
     * Creates a new ConfigFunctionlog model.
     * For ajax request will return json object
     * and for non-ajax request if creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        
    }

    /**
     * Updates an existing ConfigFunctionlog model.
     * For ajax request will return json object
     * and for non-ajax request if update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        
    }

    /**
     * Delete an existing ConfigFunctionlog model.
     * For ajax request will return json object
     * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete()
    {
        $request = Yii::$app->request;
        $id = $request->post("id");  //数组格式
        $type = $request->post("type", "hard");
        $result = false;
        //$arrIds = explode(",",$id);
        foreach ($id as $item) {
            $model = $this->findModel($item);
            switch ($type) {
                case 'hard':
                    $result =  $model->delete();
                    break;
                case 'soft':
                    $model->status = -1;
                    $result = $model->save();
                    break;
                default:
                    break;
            }

            if (!($result)) {
                return $this->asJson(ToolUtil::returnAjaxMsg(false, '删除失败'));
            }
        }


        $return = ($result) ? ToolUtil::returnAjaxMsg(true, '删除成功') : ToolUtil::returnAjaxMsg(false, '删除失败');
        return $this->asJson($return);
    }

     /**
     * Delete multiple existing ConfigFunctionlog model.
     * For ajax request will return json object
     * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionBulkdelete($type = 'hard')
    {        
        $request = Yii::$app->request;
        $pks = explode(',', $request->post( 'pks' )); // Array or selected records primary keys
        foreach ( $pks as $pk ) {
            $model = $this->findModel($pk);
            switch($type){
                case 'hard':
                    $model->delete();
                    break;
                default:
                    break;
            }
        }

        if($request->isAjax){
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ['forceClose' => true,'forceReload' => '#crud-datatable-pjax'];
        }else{
            return $this->redirect(['index']);
        }
    }


    /**
     * Finds the ConfigFunctionlog model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ConfigFunctionlog the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ConfigFunctionlog::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }


}
